<?php
		$query = "SELECT sodong FROM giaodien";
		$result = DataProvider::ExecuteQuery($query);
		$row = mysql_fetch_array($result);
		$rowsPerPage = $row['sodong'];
        if(!isset($_GET['page'])) //nếu không tồn tại page thì mặc nhiên sẽ là trang 1
		{						//vd: localhost/index.php = localhost/index.php?page=1
			$_GET['page'] = 1;
		}
		$offset = ($_GET['page']-1) * $rowsPerPage; 
        ?>
<?php
	if((isset($_REQUEST["id"]) == false) || (isset($_REQUEST["id1"]) == false))
	{
		header("location:trangchu.php");
	}
	
	$id = $_REQUEST["id"];
	$id1 = $_REQUEST["id1"];
	include_once("template/DataProvider.php");
	$query = "SELECT * FROM sanpham s, nguoidung nd WHERE s.BiXoa='0' AND nd.MaND = s.MaND AND s.MaHSX = '".$id."' AND s.MaLSP= '".$id1."'";
	$query .= " LIMIT $offset, $rowsPerPage"; //gioi han so dong hien thi moi trang 
											
	$result = DataProvider::ExecuteQuery($query);

	while($row = mysql_fetch_array($result))
	{
		include("templateSanPham.php"); 
   
	}
?>
<?php 
	//tinh tong so trang
	$sql = "SELECT COUNT(MaSP) AS numrows FROM sanpham s, nguoidung nd WHERE s.BiXoa='0' AND nd.MaND = s.MaND AND s.MaHSX = '".$id."' AND s.MaLSP= '".$id1."'";
	$result = DataProvider::ExecuteQuery($sql) ;
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$n = $row['numrows'];
	
	$tong_so_trang = ceil($n/$rowsPerPage);
	$self = $_SERVER['PHP_SELF'];//lay ten file chua doan code do (file dang xem)
								//$PHP_SELF và $_SERVER['PHP_SELF'] là hoàn toàn giống nhau	
	//gan nut BACK
	if($_GET['page'] > 1)
	echo "<a href=".$self."?act=2"."&id1=".$_REQUEST["id1"]."&id=".$_REQUEST["id"]."&page=".($_GET['page']-1).' style="background-image:url(./images/bg.jpg); color: black;"> Back'."</a>";
	//tao cac lien ket tuong ung voi cac trang 
	if($tong_so_trang > 1)//neu chi co 1 page chua san pham thi ko can phan trang
	{ 
	for($i=1 ; $i<=$tong_so_trang; $i++)
	{
		if($i == $_GET['page'])
		{
			echo ' ['.$i.']'; //in page hien tai, ko can tao lien ket
		}
		else
		//echo '<a href="'.$self.'?act=2'.'&id='.$_REQUEST["id"].'&id1='.$_REQUEST["id1"].'page='.$i.'"> Trang '.$i.' </a>';
		echo "<a href=".$self."?act=2"."&id1=".$_REQUEST["id1"]."&id=".$_REQUEST["id"]."&page=".$i.' style="color: #0661c2;"> '.$i."</a>";
	}
	}
	//gan nut NEXT
	if($_GET['page'] < $tong_so_trang)
	echo "<a href=".$self."?act=2"."&id1=".$_REQUEST["id1"]."&id=".$_REQUEST["id"]."&page=".($_GET['page']+1).' style="background-image:url(./images/bg.jpg); color: black;"> Next'."</a>";
?>
